<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="600px"
    top="5%"
  >
    <t-form ref="form" :colon="true" :data="formData" labelWidth="100px" :rules="rules">
      <t-form-item label="字典名称" name="dictName">
        <t-input clearable v-model="formData.dictName"  placeholder="请输入字典名称"></t-input>
      </t-form-item>
      <t-form-item label="字典类型" name="dictType">
        <t-input clearable v-model="formData.dictType"  placeholder="请输入字典类型"></t-input>
      </t-form-item>
      <t-form-item label="字典状态" name="status">
      <t-radio-group :options="dict.type.sys_normal_disable" v-model="formData.status">
      </t-radio-group>
      </t-form-item>
      <t-form-item label="备注" name="remark">
        <t-textarea
          v-model="formData.remark"
          placeholder="请输入内容"
        />
      </t-form-item>
    </t-form>
  </t-dialog>
</template>

<script>
import { getType, addType, updateType } from "@/api/system/dict/type";


export default {
  name: "add-edit-dict",
  dicts: ['sys_normal_disable'],
  props:["header","dictId"],
  data() {
    return {
      visible: true,
      loading: false,
      formData: {
        dictName: "",
        status: "0",
        dictType: "",
        remark: "",
      },
      rules: {
        dictName: [
          { required: true, message: "字典名称不能为空" }
        ],
        dictType: [
          { required: true, message: "字典类型不能为空" }
        ]
      }
    }
  },
  watch: {
    dictId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    }
  },
  methods:{
    getObj(){
      getType(this.dictId).then((response) => {
        this.formData = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.dictId !== 0) {
            updateType(this.formData).then((response) => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            addType(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
